package servlet;

import com.alibaba.fastjson.JSON;
import entity.Book;
import service.BookService;
import service.Impl.BookServiceImpl;
import utils.page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = "/BookServlet")
public class BookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String bookName = request.getParameter("bookName");
        String sortId = request.getParameter("sortId");
        String pageIndex = request.getParameter("pageIndex");
        if (pageIndex==null ||"".equals(pageIndex)){
            pageIndex="1";
        }

        String pageSize = request.getParameter("pageSize");
        if (pageSize==null ||"".equals(pageSize)){
            pageSize="2";
        }
        BookService service = new BookServiceImpl();
        page page = service.findpage(bookName, Integer.parseInt(sortId),
                Integer.parseInt(pageIndex),Integer.parseInt(pageSize));
        out.print(JSON.toJSONString(page));

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
